package com.qf.microuser.point.task;

import com.qf.microuser.point.service.PointRetryService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * --- 缘分让我们相遇乱世以外 ---
 *
 * @author SJY
 * 2024/12/31
 */
//@Component
public class PointTask {

    @Resource
    private RedissonClient redissonClient;

    @Resource
    private PointRetryService pointRetryService;

    @Async
    @Scheduled(cron = "0/10 * * * * ?")
    public void pointRetryTask() {
        RLock lock = redissonClient.getLock("point:retry");
        try {
            if (lock.tryLock(0, 10, TimeUnit.SECONDS)) {
                pointRetryService.pointRetryTask();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (lock.isHeldByCurrentThread() && lock.isLocked()){
                lock.unlock();
            }
        }
    }
}
